package org.xtwy.netty;

import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import org.xtwy.netty.codec.MyEncode;

/**
 * 类似于注册过滤器
 */
@Sharable
public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) {
        //添加\r\n解码器
        ch.pipeline().addLast(new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, Delimiters.lineDelimiter()[0]));
        //解码之后转换成字符串
        ch.pipeline().addLast(new StringDecoder());
        //添加自定义解码器
        ch.pipeline().addLast(new MyEncode());
        ch.pipeline().addLast(new DiscardServerHandler());
    }
}
